﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ZhongKeSite.Common;
using ZhongKeSite.Common.Helper;
using ZhongKeSite.Entity;
using ZhongKeSite.Service;
using ZhongKeSite.Web.Models;

namespace ZhongKeSite.Web.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("Admin/[controller]/[action]")]
    public class MileStoneController : BaseController
    {
        IMileStoneService mileStoneService;
        public MileStoneController(IMileStoneService _mileStoneService)
        {
            mileStoneService = _mileStoneService;
        }

        #region 里程碑管理
        [RoleAuthorize("MileStoneList")]
        public IActionResult MileStoneList(string search = "", string mileStoneType = "", int pageindex = 1, int pagesize = 15)
        {
            try
            {
                ViewBag.MileStoneTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(MileStoneType), true);
                if (Request.IsAjaxRequest())
                {
                    Expression<Func<MileStone, bool>> predicate = s => s.IsDel == 0;
                    if (!string.IsNullOrWhiteSpace(search))
                    {
                        predicate = predicate.And(k => k.Title.Contains(search));
                    }
                    if (!string.IsNullOrWhiteSpace(mileStoneType))
                    {
                        predicate = predicate.And(k => k.MileStoneType == mileStoneType);
                    }
                    return Json(mileStoneService.GetMileStonePagedList(pageindex, pagesize, predicate));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("MileStoneAdd")]
        public IActionResult MileStoneAdd()
        {
            try
            {
                MileStone model = new MileStone();
                ViewBag.MileStoneTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(MileStoneType));
                return View(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("MileStoneAdd")]
        public IActionResult MileStoneAdd(MileStone model)
        {
            try
            {
                model.AddBy = CurrentUser.Login;
                return Json(mileStoneService.AddMileStone(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageindex"></param>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("MileStoneDel")]
        public IActionResult MileStoneDel(string id)
        {
            try
            {
                List<string> ids = new List<string>() { id };
                return Json(mileStoneService.UpdateMileStoneState(ids, CurrentUser.Login, DataStatus.Deleted));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [RoleAuthorize("MileStoneEdit")]
        public IActionResult MileStoneEdit(string id)
        {
            try
            {
                ResultResponse<MileStone> result = mileStoneService.GetMileStone(id);
                ViewBag.MileStoneTypeDLL = EnumHelper.GetSelectListFromEnum(typeof(MileStoneType));
                return View(result.data);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [RoleAuthorize("MileStoneEdit")]
        public IActionResult MileStoneEdit(MileStone model)
        {
            try
            {
                model.UpdateBy = CurrentUser.Login;
                return Json(mileStoneService.UpdateMileStone(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false, msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

    }
}